﻿using FileServer.Abstractions;
using FileServer.Abstractions.Models.Options;
using FileServer.Extension.Providers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace FileServer.Extension
{
    public static class FileServerExtension
    {
        //public static void AddFileServer(this IServiceCollection services, IConfiguration configuration)
        //{
        //    FileServerOptions uploadFileOptions = configuration.GetSection("FileOptions").Get<FileServerOptions>();
        //    services.Configure<FileServerOptions>(configuration.GetSection("FileOptions"));
        //    services.BuildDefaultService();
        //    FileUploadProvider.BuildDefault();
        //    FileUploadProvider.SetUploadFileProviderOptions(uploadFileOptions.FileProviderOptions);
        //    foreach (var extension in uploadFileOptions.Extensions)
        //    {
        //        extension.AddServices(services);
        //    }
        //}

        public static void AddFileServer(this IServiceCollection services, Action<FileServerOptions> options)
        {
            FileServerOptions uploadFileOptions = new FileServerOptions();
            options(uploadFileOptions);
            foreach (var extension in uploadFileOptions.Extensions)
            {
                extension.AddServices(services);
            }
            services.Configure(options);
            services.BuildDefaultService();
            FileUploadProvider.BuildDefault();
            FileUploadProvider.SetUploadFileProviderOptions(uploadFileOptions.FileProviderOptions);
            
        }

        private static void BuildDefaultService(this IServiceCollection services)
        {
            services.AddScoped<IFileOperator, FileOperator>();
            services.AddScoped<IFileUpload, LocalFileUploadProvider>();
            services.AddScoped<IFileUpload, OssFileUploadProvider>();
        }
    }
}
